﻿using HR.Config;
using HR.Filters;
using HR.IService;
using HR.Models;
using Microsoft.AspNetCore.Mvc;

namespace HR.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IUsersService uservice;

        public HomeController(ILogger<HomeController> logger, IUsersService uservice)
        {
            _logger = logger;
            this.uservice = uservice;
        }
        /// <summary>
        /// 进入登录页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 确认登录操作
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Login([FromBody] user users)
        {
            user? ur = await uservice.LoginUrs(users);
            //_logger.LogInformation(users1.u_name + "成功登录系统！");
            //登录成功应该将对象存入到后台MVC的Session中,首页左上角显示的
            if (ur != null)
            {
                HttpContext.Session.SetString("uname", ur.u_name);
                HttpContext.Session.SetInt32("rid", ur.rid);
            }
            //var uname = HttpContext.Session.GetString("uname");
            //Console.WriteLine("成功登录系统:", uname);
            //ViewData.Model = users1;
            return Json(ur);
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult HomePage()
        {
            return View();
        }
        /// <summary>
        /// 查询到的动态菜单栏
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> HomeMenusFill(int rid)
        {
            List<MenuRole> list = await uservice.HomeMenusFill(rid);
            return Json(list);
        }
    }
}